package com.ja.openchat.ui.contact;

import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ja.openchat.R;
import com.ja.openchat.views.UserAvatarView;
import com.zy.openchat.core.model.contact.Section;
import com.zy.openchat.core.model.contact.SectionInfo;
import com.zy.openchat.core.model.user.UserInfo;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ContactChooseAdapter extends BaseSectionQuickAdapter<Section, BaseViewHolder> {

    private Map<Long,String> groupNickNames = new HashMap<>();

    public void putGroupNickname(long userId,String nickName){
        groupNickNames.put(userId,nickName);
    }

    public ContactChooseAdapter(int layoutResId, int sectionHeadResId, @Nullable List<Section> data) {
        super(sectionHeadResId, data);
        setNormalLayout(layoutResId);
    }

    @Override
    protected void convertHeader(@NotNull BaseViewHolder helper, @NotNull Section section) {
        helper.setText(R.id.iv_title, ((SectionInfo) section.getObject()).title);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder helper, Section section) {
        UserInfo user = (UserInfo) section.getObject();
        String nickName = (groupNickNames.containsKey(user.id)&&!TextUtils.isEmpty(groupNickNames.get(user.id)))?groupNickNames.get(user.id):user.displayName();
        helper.setText(R.id.iv_title, nickName);
        UserAvatarView avatarView = helper.getView(R.id.iv_icon);
        avatarView.setTextSize(17);
        avatarView.showUser(user);

        View checkView = helper.getViewOrNull(R.id.checkView);
        if (checkView != null) {
            if (mChooseListener != null) {
                checkView.setVisibility(mChooseListener.isSignalMode() ? View.GONE : View.VISIBLE);
                checkView.setSelected(mChooseListener.isChoose(user));
                helper.setGone(R.id.maskView, !mChooseListener.showMask(user));
            }
        }
    }

    private OnChooseListener mChooseListener;

    public void setOnChooseListener(OnChooseListener listener) {
        this.mChooseListener = listener;
    }

    public interface OnChooseListener {
        boolean isSignalMode();

        boolean isChoose(UserInfo user);

        boolean showMask(UserInfo user);

    }
}
